<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class SearchAction extends HomeAction
{


    public function _initialize()
	{
		parent::_initialize();
	}


    public function index()
    {

    	$keyword = isset($_REQUEST['keyword']) ? trim($_REQUEST['keyword']) : '';
    	$keyword = Input::forSearch($keyword);

    	//搜索权限检查
    	if (!$this->_userGroup['allow_search'])
    	{
    		$this->error(L('no_perms_action'));
    	}

    	if (!empty($keyword))
    	{
        	//搜索长度检查
        	if (strlen($keyword) < $this->_global['search_min_chars'] || strlen($keyword) > $this->_global['search_max_chars'])
        	{
        	    $message = sprintf(L('search_length_invalid'), $this->_global['search_min_chars'], $this->_global['search_max_chars']);
        		$this->error($message);
        	}

        	//搜索时间间隔检查
        	$lastSearch = cookie('last_search');
        	$interval = (6 == $this->_user['user_group']) ? $this->_global['search_guest_interval'] : $this->_global['search_user_interval'];
        	$allow = !$lastSearch || 0 == $interval || ($lastSearch && $lastSearch + $interval < time());
        	if (isset($_REQUEST['op']) && 'do' == $_REQUEST['op'] && !$allow)
        	{
        	    $this->assign('jumpUrl', $this->_global['site_url']);
        		$this->error(L('flood_search'));
        	}

        	//记录搜索时间
    		cookie('last_search', time());

    		$cateIDs = $parameter = '';
    		$sql = $where = $order = '';

    		if (!empty($keyword))
    		{
    			$parameter .= '&keyword='.$keyword;
    		}

    		$cateID = isset($_REQUEST['category_id']) ? intval($_REQUEST['category_id']) : 0;
        	if (!empty($cateID))
        	{
        		$cateIDs = implode(',', get_sub_category_ids($cateID, 1));
        		$where .= " AND Q.category_id IN ({$cateIDs}) ";
        		$parameter .= '&category_id='.$cateID;
        	}
        	if ('all' != $_REQUEST['qst_match_in']) 
        	{

        	}
        	if (isset($_REQUEST['status']) && 'all' != $_REQUEST['status'])
        	{
        		$where .= ' AND Q.status='.$_REQUEST['status'].' ';
        		$parameter .= '&status='.$_REQUEST['status'];
        	}
        	if (isset($_REQUEST['answer_count']) && 'all' != $_REQUEST['answer_count'])
        	{
        		$count = explode('-', $_REQUEST['answer_count']);
        		$where .= " AND Q.answer_count BETWEEN {$count[0]} AND {$count[1]} ";
        		$parameter .= '&answer_count='.$_REQUEST['answer_count'];
        	}
        	if (isset($_REQUEST['create_time']) && 'all' != $_REQUEST['create_time'])
        	{
        		$time = time() - $_REQUEST['create_time'] * 86400;
        		$where .= " AND Q.create_time>{$time} ";
        		$parameter .= '&create_time='.$_REQUEST['create_time'];
        	}

        	//结果排序

        	$Model = M('Question');
        	$prefix = C('DB_PREFIX');
        	$match  = " MATCH (title,details) AGAINST ('{$keyword}') ";

        	import('ORG.Util.Page');
			
        	$maxResults = intval($this->_global['search_max_results']);
    		$pageSize = intval($this->_global['questions_per_page']) > $maxResults ? $maxResults : $this->_global['questions_per_page'];
    		
    		$count = $Model->where("{$match} {$where}")->count();
    		$total = $maxResults > 0 && $count > $maxResults ? $maxResults : $count; 
    		$Page  = new Page($total, $pageSize);
    		$Page->parameter = $parameter;
    		$pagination = $Page->show();

    		$field = " U.user_id,U.username,U.avatar,Q.question_id,Q.category_id,Q.title,Q.answer_count,Q.star_count,Q.create_time ";
    		$sql .= " SELECT {$field},{$match} AS similar FROM {$prefix}question Q,{$prefix}user U ";
    		$sql .= " WHERE Q.poster_id=U.user_id AND {$match} {$where} ";
    		$sql .= " ORDER BY similar DESC ";
    		$sql .= " LIMIT {$Page->firstRow},{$Page->listRows} ";
    		$questions  = $Model->query($sql);
    	}

		L('search_result_for', sprintf(L('search_result_for'), $keyword));
		$seo = array(
			'title' => empty($keyword) ? L('search_advanced') : L('search_result_for'),
			'keywords' => '',
			'description' => '',
		);

		$statusOptions = array(
		    'all' => L('search_status_all'),
		    '1' => L('search_status_open'),
		    '2' => L('search_status_voting'),
		    '3' => L('search_status_resolved'),
		);
		$statusSelected = isset($_REQUEST['status']) ? $_REQUEST['status'] : 'all';
		$this->assign('statusOptions', $statusOptions);
		$this->assign('statusSelected', $statusSelected);

        $answerCountOptions = array(
		    '0' => L('search_answer_count_0'),
		    '1-3' => L('search_answer_count_1'),
		    '4-10' => L('search_answer_count_4'),
		    '11-25' => L('search_answer_count_11'),
		    '25-100' => L('search_answer_count_25'),
		    'all' => L('search_answer_count_all'),
		);
		$answerCountSelected = isset($_REQUEST['answer_count']) ? $_REQUEST['answer_count'] : 'all';
		$this->assign('answerCountOptions', $answerCountOptions);
		$this->assign('answerCountSelected', $answerCountSelected);

		$createTimeOptions = array(
		    'all' => L('search_create_time_all'),
            '1' => L('search_create_time_1'),
            '3' => L('search_create_time_3'),
            '7' => L('search_create_time_7'),
            '30' => L('search_create_time_30'),
            '90' => L('search_create_time_90'),
            '180' => L('search_create_time_180'),
            '365' => L('search_create_time_365'),
		);
		$createTimeSelected = isset($_REQUEST['create_time']) ? $_REQUEST['create_time'] : 'all';
		$this->assign('createTimeOptions', $createTimeOptions);
		$this->assign('createTimeSelected', $createTimeSelected);

    	$this->assign('seo', $seo);
    	$this->assign('breadcrumbs', $breadcrumbs);
		$this->assign('keyword', $keyword);
		$this->assign('pagination', $pagination);
		$this->assign('questions', $questions);
    	$this->display();
    }


    //google自定义搜索
    public function google()
    {
        $this->display();
    }

}


/* End of file SearchAction.class.php */
/* Location: ./App/Lib/Action/Home/SearchAction.class.php */